package com.acxy.ants.core.plugin.sqlmap;

import com.acxy.ants.common.kit.PathKit;
import com.acxy.ants.core.Plugin;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.Date;

/**
 * @author liushun
 * @version 1.0
 * @Date 2017-05-20
 */
public class SqlMapPlugin implements Plugin {

    private String dirPath;

    //是否定时检查
    private boolean timerCheck = false;

    private static DocumentBuilder documentBuilder;

    static {//调用jdk自己的xml解析，不想多加依赖。
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try {
            documentBuilder = dbf.newDocumentBuilder();
        } catch (ParserConfigurationException ex) {
            throw new RuntimeException("I don`t know .", ex);
        }
    }

    /**
     * 通过构造赋值存放sql文件目录
     *
     * @param dirPath
     */
    public SqlMapPlugin(String dirPath) {
        this.dirPath = PathKit.getClassPath() + dirPath;
    }

    /**
     * 当timerCheck时定时检查资源文件防止重启，
     * true时用于调试，正式环境请消除
     * @param dirPath   xml文件路径
     * @param timerCheck 是否定时检查
     */
    public SqlMapPlugin(String dirPath, boolean timerCheck) {
        this.dirPath = PathKit.getClassPath() + dirPath;
        this.timerCheck = timerCheck;
    }

    public boolean start() {
        if(timerCheck){
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    try {
                        while (true) {
                            Thread.sleep(2000);
                            System.out.println(new Date() + "thread timing begins to detect SqlMap resource files...");
                            parserSqlXml();
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            };
            new Thread(runnable).start();
        }else
            parserSqlXml();
        return true;
    }

    public void parserSqlXml(){
        SqlXmlParser.clear();
        File dirFile = new File(dirPath);
        if (dirFile.isDirectory()) {
            File[] files = dirFile.listFiles(new FileFilter() {
                //过滤文件取xml文件
                public boolean accept(File pathname) {
                    return pathname.getName().endsWith(".xml");
                }
            });
            for (File file : files) {
                try {
                    //迭代解析sql xml
                    Document document = documentBuilder.parse(file);
                    SqlXmlParser.parse(document);
                } catch (SAXException ex) {
                    new RuntimeException("the XML document format is incorrect!", ex);
                } catch (IOException ex) {
                    new RuntimeException("XML document read failed!", ex);
                }
            }
        }
    }

    @Override
    public boolean destroy() {
        SqlXmlParser.clear();
        return true;
    }
}
